package com.xunchi.haichuan.user.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.struts2.ServletActionContext;

import com.netai.safedoc.utility.TransactionWrapper;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.xunchi.haichuan.audit.service.LogService;
import com.xunchi.haichuan.audit.service.LogServiceImpl;
import com.xunchi.haichuan.configurator.model.SystemConfigs;
import com.xunchi.haichuan.user.model.User;
import com.xunchi.haichuan.user.service.UserService;
import com.xunchi.haichuan.user.service.UserServiceImpl;
import com.xunchi.haichuan.utility.Constants;

/**
 * 作者：kongxb<br>
 * 模块名称：用户模块<br>
 * 功能描述：用户列表<br>
 * 维护记录：2009.12.15, kongxb 创建该类
 */
public class UserListAction extends ActionSupport {

	private static final long serialVersionUID = -2275536815252535370L;

	private transient int start = 0;
	private transient int limit = 0;
	private transient boolean isSearch = false;
	private transient String userName;
	private transient String loginName;
	private transient int status = Constants.DB_NONE;
	private transient Date beginDate;
	private transient Date endDate;

	private List<Map<String, Object>> items = new ArrayList<Map<String, Object>>();
	private int totalCount;

	private UserService userService;
	private LogService logService;

	private User currentUser;

	public String execute() {
		HttpServletRequest request = ServletActionContext.getRequest();

		currentUser = (User) ActionContext.getContext().getSession().get("currentUser");
		String usrId = currentUser != null ? currentUser.getUsrId() : null;

		userService = (UserService) TransactionWrapper.decorate(new UserServiceImpl());
		logService = (LogService) TransactionWrapper.decorate(new LogServiceImpl(), true);

		try {
			List<User> list = userService.search(new Object[] { userName, loginName, status, null, 0 }, beginDate,
					endDate, start, limit);
			if (isSearch) { // 搜索
				if (SystemConfigs.isLog) {
					// logService.log("userSearch", currentUser, 0);
				}
			}
			totalCount = userService.getTotalCount();

			for (Iterator<User> iter = list.iterator(); iter.hasNext();) {
				User user = iter.next();

				Map<String, Object> map = new HashMap<String, Object>();
				map.put("id", user.getUsrId());
				map.put("name", user.getName());
				map.put("loginName", user.getLoginName());
				map.put("email", user.getEmail());
				map.put("balance", user.getBalance());
				map.put("submitTime", DateFormatUtils.format(user.getSubmitTime(), "yyyy-M-d H:mm"));
				map.put("status", user.getStatus());
				items.add(map);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return Action.SUCCESS;
	}

	/**
	 * @param start
	 *            the start to set
	 */
	public void setStart(int start) {
		this.start = start;
	}

	/**
	 * @param limit
	 *            the limit to set
	 */
	public void setLimit(int limit) {
		this.limit = limit;
	}

	/**
	 * @param isSearch
	 *            the isSearch to set
	 */
	public void setIsSearch(boolean isSearch) {
		this.isSearch = isSearch;
	}

	/**
	 * @param userName
	 *            the userName to set
	 */
	public void setUserName(String userName) {
		this.userName = userName;
	}

	/**
	 * @param loginName
	 *            the loginName to set
	 */
	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	/**
	 * @param status
	 *            the status to set
	 */
	public void setStatus(String status) {
		this.status = Integer.parseInt(status);
	}

	/**
	 * @param beginDate
	 *            the beginDate to set
	 */
	public void setBeginDate(Date beginDate) {
		this.beginDate = beginDate;
	}

	/**
	 * @param endDate
	 *            the endDate to set
	 */
	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	/**
	 * @return the items
	 */
	public List<Map<String, Object>> getItems() {
		return items;
	}

	/**
	 * @return the totalCount
	 */
	public int getTotalCount() {
		return totalCount;
	}

}
